package com.hjxr.quota.theme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjxr.quota.theme.domain.QuotaThemeSet;
import com.hjxr.quota.theme.mapper.QuotaThemeSetMapper;
import com.hjxr.quota.theme.service.IQuotaThemeSetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IQuotaThemeSetServiceImpl extends ServiceImpl<QuotaThemeSetMapper, QuotaThemeSet> implements IQuotaThemeSetService {

    @Resource
    private QuotaThemeSetMapper quotaThemeSetMapper;

    @Override
    public List<QuotaThemeSet> getSetList(Integer zoneId, Integer parentId) {
        List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectList(new QueryWrapper<QuotaThemeSet>()
                .eq("PARENT_ID", parentId).eq("ZONE_ID", zoneId));
        if (quotaThemeSets != null && quotaThemeSets.size() > 0) {
            List<Integer> ids = quotaThemeSets.stream().map(QuotaThemeSet::getId).collect(Collectors.toList());
            Map<Integer, QuotaThemeSet> nodeMap = this.baseMapper.selectList(new QueryWrapper<QuotaThemeSet>()
                    .in("PARENT_ID", ids).eq("ZONE_ID", zoneId))
                    .stream().collect(Collectors.toMap(QuotaThemeSet::getParentId, a -> a, (k1, k2) -> k1));
            quotaThemeSets.forEach(node -> {
                QuotaThemeSet quotaThemeSet = nodeMap.get(node.getId());
                node.setCategory(quotaThemeSet != null ? "主题集" : "主题");
            });
        }
        return quotaThemeSets;
    }

    @Override
    public List<QuotaThemeSet> getThemeSetByQuotaId(List<Integer> quotaIds) {
        List<QuotaThemeSet> quotaThemeSets = quotaThemeSetMapper.selectThemeSetByQuotaId(quotaIds);
        return quotaThemeSets;
    }

    @Override
    public List<QuotaThemeSet> getThemeSetTree(Integer zoneId) {
        //查询所有X
        List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectList(new QueryWrapper<QuotaThemeSet>().eq("ZONE_ID", zoneId));
        //获取树形结构
        List<QuotaThemeSet> result = quotaThemeSets.stream().filter(parentNode -> parentNode.getParentId() == 0)
                .map(nodeItem -> {
                    nodeItem.setChildren(getChildren(nodeItem, quotaThemeSets));
                    return nodeItem;
                }).sorted(((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()))).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<QuotaThemeSet> getThemeSetTreeByZoneIds(List<Integer> zoneId) {
        //查询所有X
        List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectList(new QueryWrapper<QuotaThemeSet>().in("ZONE_ID", zoneId));
        //获取树形结构
        List<QuotaThemeSet> result = quotaThemeSets.stream().filter(parentNode -> parentNode.getParentId() == 0)
                .map(nodeItem -> {
                    nodeItem.setChildren(getChildren(nodeItem, quotaThemeSets));
                    return nodeItem;
                }).sorted(((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()))).collect(Collectors.toList());
        return result;
    }

    private List<QuotaThemeSet> getChildren(QuotaThemeSet themeSet, List<QuotaThemeSet> themeSets) {
        List<QuotaThemeSet> result = themeSets.stream().filter((parent -> {
            return parent.getParentId().equals(themeSet.getId());
        })).map((children) -> {
            children.setChildren(getChildren(children, themeSets));
            return children;
        }).sorted((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum())).collect(Collectors.toList());
        return result;
    }

    @Override
    public QuotaThemeSet getInfo(Integer id) {
        QuotaThemeSet quotaThemeSet = this.baseMapper.selectById(id);
        return quotaThemeSet;
    }

    @Override
    public List<QuotaThemeSet> getThemeSetByIdIn(List<Integer> ids) {
        List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectBatchIds(ids);
        return quotaThemeSets;
    }

    @Override
    public Boolean updateThemeSet(QuotaThemeSet quotaThemeSet) {
        this.baseMapper.updateById(quotaThemeSet);
        return true;
    }

    @Override
    public Boolean removeThemeSetByIds(List<Integer> ids) {

        QueryWrapper<QuotaThemeSet> wrapper = new QueryWrapper<QuotaThemeSet>().in("PARENT_ID", ids);
        List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectList(wrapper);
        if (quotaThemeSets != null && quotaThemeSets.size() > 0) {
            return false;
        }
        this.baseMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public Boolean saveThemeSet(QuotaThemeSet quotaThemeSet) {
        try {
            if (quotaThemeSet.getParentId() != 0) {
                List<QuotaThemeSet> quotaThemeSets = this.baseMapper.selectList(new QueryWrapper<QuotaThemeSet>()
                        .eq("ID", quotaThemeSet.getParentId())
                        .eq("IS_THEME", 1));
                if (quotaThemeSets != null && quotaThemeSets.size() > 0) {
                    List<Integer> collect = quotaThemeSets.stream().map(QuotaThemeSet::getId).collect(Collectors.toList());
                    quotaThemeSetMapper.updateSetIsTheme(collect);
                }
            }
            int insert = this.baseMapper.insert(quotaThemeSet);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

